/*------------------------------------------------------------------------------
 * Copyright 2012 NCR Corporation
 *------------------------------------------------------------------------------
 * All revision information is updated automatically from source code control
 * change records - please do not manually edit.
 *------------------------------------------------------------------------------
 *
 * $LastChangedRevision:: 2008                                     $
 * $LastChangedBy:: rb250096                                       $
 * $LastChangedDate:: 2012-04-04 10:35:04 -0400 (Wed, 04 Apr 2012) $
 */

package com.ncr.nep.dashboardcore.ui.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * {@link PopupPanel} that displays a title bar at the top.
 *
 * @author rb250096
 * @version $Revision: 2008 $
 */
public class TitleBarPanel extends PopupPanel {

    //--------------------------------------------------------------------------
    //
    //  Inner classes
    //
    //--------------------------------------------------------------------------

    /**
     * Defines the style names/CSS classes for the parts of the
     * {@link TitleBarPanel}.
     */
    public class Style {

        /**
         * Gets the name of the {@link TitleBarPanel}'s style.
         *
         * @return The name of the TitleBarPanel's style.  Default is
         * <code>ds-TitleBarPanel</code>.
         */
        public String style() {
            return "ds-TitleBarPanel";
        }

        /**
         * Gets the style name for the title bar.
         *
         * @return The name of the title bar's style.  Default is
         * <code>titlebar</code>.
         */
        public String titleBarStyle() {
            return "titlebar";
        }

        /**
         * Gets the style name for the title {@link Label}.
         *
         * @return The name of the title Label's style.  Default is
         * <code>title</code>
         */
        public String titleStyle() {
            return "title";
        }

        /**
         * Gets the style name for the content area of the popup that displays
         * the {@link Widget} child.
         *
         * @return The name of the content area's style.  Default is
         * <code>content</code>.
         */
        public String contentStyle() {
            return "content";
        }
    }

    /**
     * Set of HTML templates used to generate layout.
     */
    public interface Templates extends SafeHtmlTemplates {
        @Template("<div id='{1}' class='{0}'></div><div id='{2}' class='{3}'></div>")
        SafeHtml layout(String titleBarClass,
                        String titleBarId,
                        String contentId,
                        String contentClass);
    }

    //--------------------------------------------------------------------------
    //
    //  Fields
    //
    //--------------------------------------------------------------------------

    /**
     * The child Widget displayed in the popup.
     */
    protected Widget childWidget;

    /**
     * Layout panel used to layout titlebar and content areas.
     */
    protected HTMLPanel layoutContainer;

    /**
     * The unique ID of the title bar div.
     */
    protected String titleBarId;

    /**
     * The unique ID of the content area div.
     */
    protected String contentId;

    /**
     * The title bar Label used to display title of popup.
     */
    protected Label titleLabel;

    /**
     * Style object containing names of styles for control.
     */
    protected Style style = new Style();

    /**
     * Used to generate HTML from templates.
     */
    protected Templates templates = GWT.create(Templates.class);

    //--------------------------------------------------------------------------
    //
    //  Constructors
    //
    //--------------------------------------------------------------------------

    /**
     * Constructor.
     */
    public TitleBarPanel() {
        this(null);
    }

    /**
     * Constructor.
     *
     * @param style Overrides the default style names used for the popup.
     */
    public TitleBarPanel(Style style) {
        super();

        if(style != null) {
            this.style = style;
        }

        this.titleBarId = HTMLPanel.createUniqueId();
        this.contentId = HTMLPanel.createUniqueId();

        this.layoutContainer = new HTMLPanel(templates.layout(
                this.style.titleBarStyle(),
                this.titleBarId,
                this.contentId,
                this.style.contentStyle()));
        this.layoutContainer.setStyleName(this.style.style());

        this.titleLabel = new Label();
        this.titleLabel.setStyleName(this.style.titleStyle());

        this.layoutContainer.add(this.titleLabel, this.titleBarId);

        super.setWidget(this.layoutContainer);
    }

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    @Override
    public void setTitle(String title) {
        this.titleLabel.setText(title);
    }

    @Override
    public void setWidget(Widget w) {
        if(this.childWidget != null) {
            this.childWidget.removeFromParent();
        }

        this.layoutContainer.add(w, this.contentId);
        this.childWidget = w;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     * Centers the popup horizontally and sets the top position to a small
     * margin below the top of the viewport.
     */
    public void centerTopBiased() {
        this.show();

        int clientWidth = Window.getClientWidth();
        int elementWidth = this.getElement().getClientWidth();
        int left = (clientWidth - elementWidth) / 2;
        int top = 50;

        this.setPopupPosition(Math.max(Window.getScrollLeft() + left, 0), top);

        this.setVisible(true);
    }
}
